﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ConferenceZoom.Models;
using IndicoInterface;
using System.IO;
using ConferenceZoom.DTDBService;
using System.Configuration;

namespace ConferenceZoom.Controllers
{
    /// <summary>
    /// Deals with individual conferences
    /// </summary>
    [HandleError]
    public class ConferenceController : Controller
    {
        /// <summary>
        /// This guy isn't real - so we go back.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Does its best to render a Conference. If the talk is already there, it
        /// will just show it.
        /// 
        ///   /Conference/Render/url
        /// 
        /// We look up the URL, and if it isn't there, then we submit a render for it, and then
        /// do our best to show the thing. :-)
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ActionResult Render(string url)
        {
            ///
            /// Convert the URL into a unique string
            /// 

            string agendaXML;
            try
            {
                AgendaInfo ai = new AgendaInfo(url);
                StringWriter sw = new StringWriter();
                ai.Seralize(sw);
                agendaXML = sw.ToString();

                if (agendaXML == null || agendaXML.Length == 0)
                {
                    ///
                    /// Unable to parse it -- we give up at this point...
                    /// 

                    return View("BadURLToRender");
                }
            }
            catch (Exception)
            {
                /// TODO: Log this
                return View("BadURLToRender");
            }

            ///
            /// See if we can find it. Submit it as a render if it isn't in there already.
            /// 

            int id = -1;
            using (DTDBClient tcl = new DTDBClient())
            {
                id = tcl.FindTalk(agendaXML);
                if (id == -1)
                {
                    tcl.SubmitNewTalk(agendaXML, "Waiting For Rendering To Start", DateTime.Now);
                    id = tcl.FindTalk(agendaXML);
                }
            }

            ///
            /// Now display what we know!
            /// 

            return RedirectToAction("Show", new { id = id });
        }

        /// <summary>
        /// Find a conference and show it. Lookup can occur by URL or by ID.
        /// 
        /// Conference/Show?url=indico.cern.ch/confID=55
        /// Conference/Show?id=55
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ActionResult Show(string url, int? id)
        {
            ///
            /// Get a conference object with all of the data.
            /// 

            using (DTDBClient tcb = new DTDBClient())
            {
                if (id == null || !id.HasValue)
                {
                    ///
                    /// We have only the URL. So we will see if we can turn it into
                    /// an ID. If it isn't an ID, then we will submit it to be rendered.
                    /// 
                    try
                    {
                        AgendaInfo ai = new AgendaInfo(url);
                        StringWriter sw = new StringWriter();
                        ai.Seralize(sw);
                        string agendaXML = sw.ToString();

                        id = tcb.FindTalk(agendaXML);

                        if (id == -1)
                        {
                            return RedirectToAction("Render");
                        }
                    }
                    catch (Exception)
                    {
                        return View("BadURLToShow");
                    }
                }

                ///
                /// Check to see if the ID even exists
                /// 

                if (!tcb.ValidTalkID(id.Value))
                {
                    return View("BadIDToShow");
                }

                ///
                /// At this point we should have a valid ID of the talk. See if we can
                /// get its state
                /// 

                if (tcb.CanBeViewed(id.Value))
                {
                    return Redirect(ConfigurationSettings.AppSettings["DZURLBase"] + "/" + id.Value.ToString("00000000") + "/ViewConference.html");
                }
                else
                {
                    ViewData["Message"] = "Talk is still being rendered...";
                    return View("ShowConferencePending");
                }
            }
        }
    }
}
